/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Description: Little Test主程序 - 音频采集、处理、编解码和播放的主控制逻辑
 */

#include "little_test.h"
#include "osal_debug.h"
#include "osal_task.h"
#include "osal_msgqueue.h"
#include "osal_semaphore.h"
#include "securec.h"
#include <stdlib.h>
#include <string.h>

// 全局上下文
static little_test_context_t g_little_test_ctx = {0};

// 任务句柄
static osal_task *g_capture_task = NULL;
static osal_task *g_processing_task = NULL;
static osal_task *g_encode_task = NULL;
static osal_task *g_decode_task = NULL;
static osal_task *g_playback_task = NULL;

// 消息队列
static osal_msgqueue g_raw_frame_queue;      // 原始音频帧队列
static osal_msgqueue g_processed_frame_queue; // 预处理后音频帧队列
static osal_msgqueue g_encoded_packet_queue;  // 编码数据包队列
static osal_msgqueue g_decoded_frame_queue;   // 解码音频帧队列

// 控制信号量
static osal_sem g_system_ready_sem;  // 系统就绪信号量
static bool g_system_running = false;

/**
 * @brief 音频采集任务
 * @param arg 任务参数
 */
static void *audio_capture_task(void *arg)
{
    little_test_context_t *ctx = (little_test_context_t *)arg;
    audio_frame_t frame;
    int ret;
    
    osal_printk("[CAPTURE_TASK] Started\n");
    
    while (g_system_running) {
        // 等待音频数据就绪
        ret = audio_capture_wait_data(&ctx->capture, 100);  // 100ms超时
        if (ret == -2) {
            // 超时，继续等待
            continue;
        } else if (ret != 0) {
            osal_printk("[CAPTURE_TASK] Wait data failed: %d\n", ret);
            osal_msleep(10);
            continue;
        }
        
        // 获取音频帧
        ret = audio_capture_get_frame(&ctx->capture, &frame);
        if (ret != 0) {
            osal_printk("[CAPTURE_TASK] Get frame failed: %d\n", ret);
            continue;
        }
        
        // 将音频帧发送到处理队列
        ret = osal_msg_queue_write(&g_raw_frame_queue, &frame, sizeof(audio_frame_t), 0);
        if (ret != OSAL_SUCCESS) {
            osal_printk("[CAPTURE_TASK] Queue write failed\n");
            // 队列满，丢弃旧数据
            audio_frame_t dummy;
            osal_msg_queue_read(&g_raw_frame_queue, &dummy, sizeof(audio_frame_t), 0);
            osal_msg_queue_write(&g_raw_frame_queue, &frame, sizeof(audio_frame_t), 0);
        }
    }
    
    osal_printk("[CAPTURE_TASK] Stopped\n");
    return NULL;
}

/**
 * @brief 音频预处理任务
 * @param arg 任务参数
 */
static void *audio_processing_task(void *arg)
{
    little_test_context_t *ctx = (little_test_context_t *)arg;
    audio_frame_t input_frame, output_frame;
    int ret;
    
    osal_printk("[PROCESSING_TASK] Started\n");
    
    while (g_system_running) {
        // 从原始帧队列读取数据
        ret = osal_msg_queue_read(&g_raw_frame_queue, &input_frame, sizeof(audio_frame_t), 100);
        if (ret != OSAL_SUCCESS) {
            // 超时或队列空，继续等待
            continue;
        }
        
        // 处理音频帧
        ret = audio_processing_process_frame(&ctx->processing, &input_frame, &output_frame);
        if (ret != 0) {
            osal_printk("[PROCESSING_TASK] Process frame failed: %d\n", ret);
            continue;
        }
        
        // 将处理后的音频帧发送到编码队列
        ret = osal_msg_queue_write(&g_processed_frame_queue, &output_frame, sizeof(audio_frame_t), 0);
        if (ret != OSAL_SUCCESS) {
            osal_printk("[PROCESSING_TASK] Queue write failed\n");
            // 队列满，丢弃旧数据
            audio_frame_t dummy;
            osal_msg_queue_read(&g_processed_frame_queue, &dummy, sizeof(audio_frame_t), 0);
            osal_msg_queue_write(&g_processed_frame_queue, &output_frame, sizeof(audio_frame_t), 0);
        }
    }
    
    osal_printk("[PROCESSING_TASK] Stopped\n");
    return NULL;
}

/**
 * @brief Opus编码任务
 * @param arg 任务参数
 */
static void *opus_encode_task(void *arg)
{
    little_test_context_t *ctx = (little_test_context_t *)arg;
    audio_frame_t frame;
    encoded_packet_t packet;
    int ret;
    
    osal_printk("[ENCODE_TASK] Started\n");
    
    while (g_system_running) {
        // 从处理后帧队列读取数据
        ret = osal_msg_queue_read(&g_processed_frame_queue, &frame, sizeof(audio_frame_t), 100);
        if (ret != OSAL_SUCCESS) {
            // 超时或队列空，继续等待
            continue;
        }
        
        // 编码音频帧
        packet.size = opus_encode_frame(&ctx->opus_codec, 
                                       (const int16_t *)frame.data, 
                                       packet.data, 
                                       LITTLE_TEST_MAX_PACKET_SIZE);
        
        if (packet.size < 0) {
            osal_printk("[ENCODE_TASK] Encode failed: %d\n", packet.size);
            continue;
        }
        
        if (packet.size == 0) {
            // DTX帧，跳过
            continue;
        }
        
        packet.timestamp = frame.timestamp;
        packet.sequence = frame.sequence;
        
        // 将编码数据包发送到解码队列
        ret = osal_msg_queue_write(&g_encoded_packet_queue, &packet, sizeof(encoded_packet_t), 0);
        if (ret != OSAL_SUCCESS) {
            osal_printk("[ENCODE_TASK] Queue write failed\n");
            // 队列满，丢弃旧数据
            encoded_packet_t dummy;
            osal_msg_queue_read(&g_encoded_packet_queue, &dummy, sizeof(encoded_packet_t), 0);
            osal_msg_queue_write(&g_encoded_packet_queue, &packet, sizeof(encoded_packet_t), 0);
        }
    }
    
    osal_printk("[ENCODE_TASK] Stopped\n");
    return NULL;
}

/**
 * @brief Opus解码任务
 * @param arg 任务参数
 */
static void *opus_decode_task(void *arg)
{
    little_test_context_t *ctx = (little_test_context_t *)arg;
    encoded_packet_t packet;
    audio_frame_t frame;
    int ret;
    
    osal_printk("[DECODE_TASK] Started\n");
    
    while (g_system_running) {
        // 从编码数据包队列读取数据
        ret = osal_msg_queue_read(&g_encoded_packet_queue, &packet, sizeof(encoded_packet_t), 100);
        if (ret != OSAL_SUCCESS) {
            // 超时或队列空，继续等待
            continue;
        }
        
        // 解码数据包
        ret = opus_decode_frame(&ctx->opus_codec, 
                               packet.data, 
                               packet.size, 
                               (int16_t *)ctx->opus_codec.decode_buffer);
        
        if (ret < 0) {
            osal_printk("[DECODE_TASK] Decode failed: %d\n", ret);
            continue;
        }
        
        // 设置解码后的音频帧
        frame.data = ctx->opus_codec.decode_buffer;
        frame.size = ret * sizeof(int16_t);
        frame.timestamp = packet.timestamp;
        frame.sequence = packet.sequence;
        
        // 将解码后的音频帧发送到播放队列
        ret = osal_msg_queue_write(&g_decoded_frame_queue, &frame, sizeof(audio_frame_t), 0);
        if (ret != OSAL_SUCCESS) {
            osal_printk("[DECODE_TASK] Queue write failed\n");
            // 队列满，丢弃旧数据
            audio_frame_t dummy;
            osal_msg_queue_read(&g_decoded_frame_queue, &dummy, sizeof(audio_frame_t), 0);
            osal_msg_queue_write(&g_decoded_frame_queue, &frame, sizeof(audio_frame_t), 0);
        }
    }
    
    osal_printk("[DECODE_TASK] Stopped\n");
    return NULL;
}

/**
 * @brief 音频播放任务
 * @param arg 任务参数
 */
static void *audio_playback_task(void *arg)
{
    little_test_context_t *ctx = (little_test_context_t *)arg;
    audio_frame_t frame;
    int ret;
    
    osal_printk("[PLAYBACK_TASK] Started\n");
    
    while (g_system_running) {
        // 等待播放缓冲区就绪
        ret = audio_playback_wait_buffer(&ctx->playback, 100);  // 100ms超时
        if (ret == -2) {
            // 超时，播放静音
            audio_playback_play_silence(&ctx->playback);
            continue;
        } else if (ret != 0) {
            osal_printk("[PLAYBACK_TASK] Wait buffer failed: %d\n", ret);
            osal_msleep(10);
            continue;
        }
        
        // 从解码帧队列读取数据
        ret = osal_msg_queue_read(&g_decoded_frame_queue, &frame, sizeof(audio_frame_t), 0);
        if (ret != OSAL_SUCCESS) {
            // 队列空，播放静音
            audio_playback_play_silence(&ctx->playback);
            continue;
        }
        
        // 播放音频帧
        ret = audio_playback_play_frame(&ctx->playback, &frame);
        if (ret != 0) {
            osal_printk("[PLAYBACK_TASK] Play frame failed: %d\n", ret);
            // 播放失败，播放静音
            audio_playback_play_silence(&ctx->playback);
        }
    }
    
    osal_printk("[PLAYBACK_TASK] Stopped\n");
    return NULL;
}

/**
 * @brief 初始化消息队列
 * @return 0成功，其他失败
 */
static int little_test_init_queues(void)
{
    errcode_t ret;
    
    // 创建原始音频帧队列
    ret = osal_msg_queue_create(&g_raw_frame_queue, 
                               "raw_frame_queue", 
                               sizeof(audio_frame_t), 
                               LITTLE_TEST_QUEUE_SIZE);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create raw frame queue\n");
        return -1;
    }
    
    // 创建处理后音频帧队列
    ret = osal_msg_queue_create(&g_processed_frame_queue, 
                               "processed_frame_queue", 
                               sizeof(audio_frame_t), 
                               LITTLE_TEST_QUEUE_SIZE);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create processed frame queue\n");
        osal_msg_queue_delete(&g_raw_frame_queue);
        return -1;
    }
    
    // 创建编码数据包队列
    ret = osal_msg_queue_create(&g_encoded_packet_queue, 
                               "encoded_packet_queue", 
                               sizeof(encoded_packet_t), 
                               LITTLE_TEST_QUEUE_SIZE);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create encoded packet queue\n");
        osal_msg_queue_delete(&g_raw_frame_queue);
        osal_msg_queue_delete(&g_processed_frame_queue);
        return -1;
    }
    
    // 创建解码音频帧队列
    ret = osal_msg_queue_create(&g_decoded_frame_queue, 
                               "decoded_frame_queue", 
                               sizeof(audio_frame_t), 
                               LITTLE_TEST_QUEUE_SIZE);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create decoded frame queue\n");
        osal_msg_queue_delete(&g_raw_frame_queue);
        osal_msg_queue_delete(&g_processed_frame_queue);
        osal_msg_queue_delete(&g_encoded_packet_queue);
        return -1;
    }
    
    osal_printk("[LITTLE_TEST] Message queues created successfully\n");
    return 0;
}

/**
 * @brief 反初始化消息队列
 */
static void little_test_deinit_queues(void)
{
    osal_msg_queue_delete(&g_raw_frame_queue);
    osal_msg_queue_delete(&g_processed_frame_queue);
    osal_msg_queue_delete(&g_encoded_packet_queue);
    osal_msg_queue_delete(&g_decoded_frame_queue);
    
    osal_printk("[LITTLE_TEST] Message queues destroyed\n");
}

/**
 * @brief 创建所有任务
 * @return 0成功，其他失败
 */
static int little_test_create_tasks(void)
{
    osal_task_attr attr = {0};
    errcode_t ret;
    
    // 设置任务属性
    attr.stack_size = LITTLE_TEST_TASK_STACK_SIZE;
    attr.priority = LITTLE_TEST_TASK_PRIORITY;
    
    // 创建音频采集任务
    attr.name = "capture_task";
    ret = osal_task_create(&g_capture_task, &attr, audio_capture_task, &g_little_test_ctx);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create capture task\n");
        return -1;
    }
    
    // 创建音频预处理任务
    attr.name = "processing_task";
    ret = osal_task_create(&g_processing_task, &attr, audio_processing_task, &g_little_test_ctx);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create processing task\n");
        osal_task_destroy(g_capture_task);
        return -1;
    }
    
    // 创建Opus编码任务
    attr.name = "encode_task";
    ret = osal_task_create(&g_encode_task, &attr, opus_encode_task, &g_little_test_ctx);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create encode task\n");
        osal_task_destroy(g_capture_task);
        osal_task_destroy(g_processing_task);
        return -1;
    }
    
    // 创建Opus解码任务
    attr.name = "decode_task";
    ret = osal_task_create(&g_decode_task, &attr, opus_decode_task, &g_little_test_ctx);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create decode task\n");
        osal_task_destroy(g_capture_task);
        osal_task_destroy(g_processing_task);
        osal_task_destroy(g_encode_task);
        return -1;
    }
    
    // 创建音频播放任务
    attr.name = "playback_task";
    ret = osal_task_create(&g_playback_task, &attr, audio_playback_task, &g_little_test_ctx);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create playback task\n");
        osal_task_destroy(g_capture_task);
        osal_task_destroy(g_processing_task);
        osal_task_destroy(g_encode_task);
        osal_task_destroy(g_decode_task);
        return -1;
    }
    
    osal_printk("[LITTLE_TEST] All tasks created successfully\n");
    return 0;
}

/**
 * @brief 销毁所有任务
 */
static void little_test_destroy_tasks(void)
{
    if (g_capture_task != NULL) {
        osal_task_destroy(g_capture_task);
        g_capture_task = NULL;
    }
    
    if (g_processing_task != NULL) {
        osal_task_destroy(g_processing_task);
        g_processing_task = NULL;
    }
    
    if (g_encode_task != NULL) {
        osal_task_destroy(g_encode_task);
        g_encode_task = NULL;
    }
    
    if (g_decode_task != NULL) {
        osal_task_destroy(g_decode_task);
        g_decode_task = NULL;
    }
    
    if (g_playback_task != NULL) {
        osal_task_destroy(g_playback_task);
        g_playback_task = NULL;
    }
    
    osal_printk("[LITTLE_TEST] All tasks destroyed\n");
}

/**
 * @brief 初始化Little Test系统
 * @return 0成功，其他失败
 */
int little_test_init(void)
{
    int ret;
    
    osal_printk("[LITTLE_TEST] Initializing...\n");
    
    // 初始化上下文
    memset_s(&g_little_test_ctx, sizeof(little_test_context_t), 0, sizeof(little_test_context_t));
    
    // 创建系统就绪信号量
    ret = osal_sem_init(&g_system_ready_sem, 0);
    if (ret != OSAL_SUCCESS) {
        osal_printk("[LITTLE_TEST] Failed to create system ready semaphore\n");
        return -1;
    }
    
    // 初始化循环缓冲区
    ret = circular_buffer_init(&g_little_test_ctx.raw_buffer);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to init raw buffer\n");
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    ret = circular_buffer_init(&g_little_test_ctx.processed_buffer);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to init processed buffer\n");
        circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    // 初始化音频采集
    ret = audio_capture_init(&g_little_test_ctx.capture);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to init audio capture\n");
        circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
        circular_buffer_deinit(&g_little_test_ctx.processed_buffer);
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    // 初始化音频播放
    ret = audio_playback_init(&g_little_test_ctx.playback);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to init audio playback\n");
        audio_capture_deinit(&g_little_test_ctx.capture);
        circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
        circular_buffer_deinit(&g_little_test_ctx.processed_buffer);
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    // 初始化音频预处理
    ret = audio_processing_init(&g_little_test_ctx.processing);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to init audio processing\n");
        audio_playback_deinit(&g_little_test_ctx.playback);
        audio_capture_deinit(&g_little_test_ctx.capture);
        circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
        circular_buffer_deinit(&g_little_test_ctx.processed_buffer);
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    // 初始化Opus编解码器
    ret = opus_codec_init(&g_little_test_ctx.opus_codec);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to init Opus codec\n");
        audio_processing_deinit(&g_little_test_ctx.processing);
        audio_playback_deinit(&g_little_test_ctx.playback);
        audio_capture_deinit(&g_little_test_ctx.capture);
        circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
        circular_buffer_deinit(&g_little_test_ctx.processed_buffer);
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    // 初始化消息队列
    ret = little_test_init_queues();
    if (ret != 0) {
        opus_codec_deinit(&g_little_test_ctx.opus_codec);
        audio_processing_deinit(&g_little_test_ctx.processing);
        audio_playback_deinit(&g_little_test_ctx.playback);
        audio_capture_deinit(&g_little_test_ctx.capture);
        circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
        circular_buffer_deinit(&g_little_test_ctx.processed_buffer);
        osal_sem_destroy(&g_system_ready_sem);
        return -1;
    }
    
    g_little_test_ctx.initialized = true;
    
    osal_printk("[LITTLE_TEST] Initialized successfully\n");
    osal_printk("[LITTLE_TEST] Sample rate: %d Hz, Frame size: %d samples\n", 
                LITTLE_TEST_SAMPLE_RATE, LITTLE_TEST_FRAME_SIZE);
    osal_printk("[LITTLE_TEST] Buffer count: %d, Opus bitrate: %d bps\n", 
                LITTLE_TEST_BUFFER_COUNT, LITTLE_TEST_OPUS_BITRATE);
    
    return 0;
}

/**
 * @brief 反初始化Little Test系统
 */
void little_test_deinit(void)
{
    if (!g_little_test_ctx.initialized) {
        return;
    }
    
    // 停止系统
    little_test_stop();
    
    // 反初始化消息队列
    little_test_deinit_queues();
    
    // 反初始化各模块
    opus_codec_deinit(&g_little_test_ctx.opus_codec);
    audio_processing_deinit(&g_little_test_ctx.processing);
    audio_playback_deinit(&g_little_test_ctx.playback);
    audio_capture_deinit(&g_little_test_ctx.capture);
    circular_buffer_deinit(&g_little_test_ctx.raw_buffer);
    circular_buffer_deinit(&g_little_test_ctx.processed_buffer);
    
    // 销毁信号量
    osal_sem_destroy(&g_system_ready_sem);
    
    g_little_test_ctx.initialized = false;
    
    osal_printk("[LITTLE_TEST] Deinitialized\n");
}

/**
 * @brief 启动Little Test系统
 * @return 0成功，其他失败
 */
int little_test_start(void)
{
    int ret;
    
    if (!g_little_test_ctx.initialized) {
        osal_printk("[LITTLE_TEST] Not initialized\n");
        return -1;
    }
    
    if (g_system_running) {
        osal_printk("[LITTLE_TEST] Already running\n");
        return 0;
    }
    
    osal_printk("[LITTLE_TEST] Starting...\n");
    
    // 启动音频采集
    ret = audio_capture_start(&g_little_test_ctx.capture);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to start audio capture\n");
        return -1;
    }
    
    // 启动音频播放
    ret = audio_playback_start(&g_little_test_ctx.playback);
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Failed to start audio playback\n");
        audio_capture_stop(&g_little_test_ctx.capture);
        return -1;
    }
    
    // 设置系统运行标志
    g_system_running = true;
    
    // 创建所有任务
    ret = little_test_create_tasks();
    if (ret != 0) {
        g_system_running = false;
        audio_playback_stop(&g_little_test_ctx.playback);
        audio_capture_stop(&g_little_test_ctx.capture);
        return -1;
    }
    
    // 释放系统就绪信号量
    osal_sem_up(&g_system_ready_sem);
    
    osal_printk("[LITTLE_TEST] Started successfully\n");
    return 0;
}

/**
 * @brief 停止Little Test系统
 * @return 0成功，其他失败
 */
int little_test_stop(void)
{
    if (!g_system_running) {
        return 0;
    }
    
    osal_printk("[LITTLE_TEST] Stopping...\n");
    
    // 设置停止标志
    g_system_running = false;
    
    // 等待任务结束
    osal_msleep(200);
    
    // 销毁所有任务
    little_test_destroy_tasks();
    
    // 停止音频采集和播放
    audio_capture_stop(&g_little_test_ctx.capture);
    audio_playback_stop(&g_little_test_ctx.playback);
    
    osal_printk("[LITTLE_TEST] Stopped\n");
    return 0;
}

/**
 * @brief 获取系统统计信息
 * @param stats 输出统计信息
 * @return 0成功，其他失败
 */
int little_test_get_stats(little_test_stats_t *stats)
{
    if (!g_little_test_ctx.initialized || stats == NULL) {
        return -1;
    }
    
    memset_s(stats, sizeof(little_test_stats_t), 0, sizeof(little_test_stats_t));
    
    // 获取采集统计
    audio_capture_get_stats(&g_little_test_ctx.capture, 
                           &stats->frames_captured, 
                           &stats->capture_errors, 
                           NULL);
    
    // 获取播放统计
    audio_playback_get_stats(&g_little_test_ctx.playback, 
                            &stats->frames_played, 
                            &stats->playback_errors, 
                            &stats->underrun_count, 
                            NULL);
    
    // 获取处理统计
    audio_processing_get_stats(&g_little_test_ctx.processing, 
                              &stats->peak_level, 
                              &stats->rms_level, 
                              &stats->current_gain);
    
    stats->system_running = g_system_running;
    
    return 0;
}

/**
 * @brief Little Test主函数
 */
void little_test_main(void)
{
    int ret;
    little_test_stats_t stats;
    uint32_t stats_counter = 0;
    
    osal_printk("\n=== Little Test Audio System ===\n");
    osal_printk("Audio capture, processing, Opus codec and playback demo\n");
    osal_printk("Sample rate: %d Hz, Frame size: %d samples (%.1f ms)\n", 
                LITTLE_TEST_SAMPLE_RATE, LITTLE_TEST_FRAME_SIZE,
                (float)LITTLE_TEST_FRAME_SIZE * 1000.0f / LITTLE_TEST_SAMPLE_RATE);
    osal_printk("Opus bitrate: %d bps, Buffer count: %d\n", 
                LITTLE_TEST_OPUS_BITRATE, LITTLE_TEST_BUFFER_COUNT);
    osal_printk("================================\n\n");
    
    // 初始化系统
    ret = little_test_init();
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Initialization failed\n");
        return;
    }
    
    // 启动系统
    ret = little_test_start();
    if (ret != 0) {
        osal_printk("[LITTLE_TEST] Start failed\n");
        little_test_deinit();
        return;
    }
    
    // 等待系统就绪
    osal_sem_down(&g_system_ready_sem);
    
    osal_printk("[LITTLE_TEST] System is running. Press any key to stop...\n");
    
    // 主循环：定期打印统计信息
    while (g_system_running) {
        osal_msleep(5000);  // 每5秒打印一次统计信息
        
        stats_counter++;
        if (stats_counter % 1 == 0) {  // 每5秒打印一次
            ret = little_test_get_stats(&stats);
            if (ret == 0) {
                osal_printk("\n[STATS] Captured: %d, Played: %d, Errors: C=%d/P=%d, Underruns: %d\n", 
                            stats.frames_captured, stats.frames_played, 
                            stats.capture_errors, stats.playback_errors, stats.underrun_count);
                osal_printk("[STATS] Audio: Peak=%d, RMS=%d, AGC_Gain=%.2f\n", 
                            stats.peak_level, stats.rms_level, stats.current_gain);
            }
        }
        
        // 检查是否需要停止（这里可以添加按键检测或其他停止条件）
        // 为了演示，运行60秒后自动停止
        if (stats_counter >= 12) {  // 60秒
            osal_printk("\n[LITTLE_TEST] Demo completed, stopping...\n");
            break;
        }
    }
    
    // 停止系统
    little_test_stop();
    
    // 反初始化
    little_test_deinit();
    
    osal_printk("\n[LITTLE_TEST] Demo finished\n");
}